<?php
require_once dirname(__FILE__) . '/common.inc.php';

header('Content-Type: text/plain');

$myPdo = DbUtils::createPdoInst();

$action = MiscUtils::getParam('action', NULL);
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'profile':
      profile($myPdo);
      break;
    case 'signin':
      signin($myPdo);
      break;
    case 'homepage':
      homepage($myPdo);
      break;
    case 'menu':
      menu($myPdo);
      break;
    case 'rearrange':
      rearrange($myPdo);
      break;
    case 'delete_pages':
      delete_pages($myPdo);
      break;
    case 'delete_instances':
      delete_instances($myPdo);
      break;
    case 'default_theme':
      default_theme($myPdo);
      break;
    case 'delete_themes':
      delete_themes($myPdo);
      break;
    case 'delete_plugins':
      delete_plugins($myPdo);
      break;
    case 'delete_modules':
      delete_modules($myPdo);
      break;
    case 'preview':
      preview($myPdo);
      break;
    case 'ezobj':
      ezobj($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo ERROR_SIGN . $e->getMessage();
}

function profile($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  if (isset($q->_newpass) && !empty($q->_newpass)) {
    $q->password = md5($q->_newpass);
  }
  if (!isset($q->id)) {
    DbUtils::add($myPdo, 'pz_profile', $q);
  } else {
    DbUtils::update($myPdo, 'pz_profile', $q);
  }
  echo json_encode(array());
}

function signin($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.account = :v1 AND t.password = :v2';
  $cond_vals->v = array(':v1' => $q->account, ':v2' => md5($q->password));
  $result = DbUtils::get($myPdo, 'pz_profile', $cond_vals, NULL, NULL, NULL, NULL, NULL);
  if (count($result->d) == 1) {
    $profile = $result->d[0];
    
    $_SESSION[USER] = $profile;
    setcookie(USER, json_encode($_SESSION[USER]), (time() + 3600), PATH, DOMAIN);
    echo json_encode($profile);
  } else {
    echo json_encode(array());
  }
}

function homepage($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  
  $myPdo->exec('UPDATE `pz_menu` SET is_default = 0');
  DbUtils::update($myPdo, 'pz_menu', $q);
  echo json_encode(array());
}

function menu($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  
  if ($q->is_default) {
    $myPdo->exec('UPDATE `pz_menu` SET is_default = 0');
  }
  if (isset($q->id)) {
    DbUtils::update($myPdo, 'pz_menu', $q);
  } else {
    DbUtils::add($myPdo, 'pz_menu', $q);
  }
  echo json_encode(array('id' => $myPdo->lastInsertId()));
}

function rearrange($myPdo) {
  $q = MiscUtils::getParam('q', NULL);
  if ($q) {
    $result = DbUtils::get($myPdo, $q, NULL, 't.order', 'ASC', NULL, NULL, NULL)->d;
    foreach ($result as $idx => $tmp) {
      $tmp->order += ($idx + 1) * 33.33;
      DbUtils::update($myPdo, $q, $tmp);
    }
    echo json_encode($result);
  }
}

function delete_pages($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', array()));
  foreach ($q as &$tmp) {
    DbUtils::delete($myPdo, 'pz_menu', $tmp);
    $cond_vals = new stdClass();
    $cond_vals->c = 'menu_id = :v1';
    $cond_vals->v = array(':v1' => $tmp->id);
    DbUtils::deleteByCond($myPdo, 'pz_page', $cond_vals);
  }
  echo json_encode(array());
}

function delete_instances($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', array()));
  foreach ($q as &$tmp) {
    DbUtils::delete($myPdo, 'pz_plugin_instance', $tmp);
  }
  
  echo json_encode(array());
}

function default_theme($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', array()));
  DbUtils::update($myPdo, 'pz_profile', $q);
  if (isset($_SESSION[USER])) {
    $_SESSION[USER]->theme_id = $q->theme_id;
    setcookie(USER, json_encode($_SESSION[USER]), (time() + 3600), PATH, DOMAIN);
  }
  echo json_encode(array());
}

function delete_themes($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', array()));
  foreach ($q as &$tmp) {
    MiscUtils::rrmdir("ext/ui/bin/$tmp->folder");
    DbUtils::delete($myPdo, 'pz_theme', $tmp);
  }
  echo json_encode(array());
}

function delete_plugins($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', array()));
  foreach ($q as &$tmp) {
    MiscUtils::rrmdir("ext/plugin/bin/$tmp->folder");
    DbUtils::delete($myPdo, 'pz_plugin', $tmp);
    
    $cond_vals = new stdClass();
    $cond_vals->c = 'plugin_id = :v1';
    $cond_vals->v = array(':v1' => $tmp->id);
    DbUtils::deleteByCond($myPdo, 'pz_plugin_instance', $cond_vals);
  }
  echo json_encode(array());
}

function delete_modules($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', array()));
  foreach ($q as &$tmp) {
    MiscUtils::rrmdir("ext/module/bin/$tmp->folder");
    DbUtils::delete($myPdo, 'pz_module', $tmp);
    
    $cond_vals = new stdClass();
    $cond_vals->c = 'module_track_id = :v1';
    $cond_vals->v = array(':v1' => $tmp->track_id);
    DbUtils::deleteByCond($myPdo, 'pz_module_menu', $cond_vals);
  }
  echo json_encode(array());
}

function preview($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', array()));
  $_SESSION[PREVIEW] = $q;
  echo json_encode(array());
}

function ezobj($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  
  if (isset($q->obj->id)) {
    DbUtils::update($myPdo, $q->tbl, $q->obj);
  } else {
    DbUtils::add($myPdo, $q->tbl, $q->obj);
  }
  $q->obj->id = $myPdo->lastInsertId();
  echo json_encode($q->obj);
}
?>